/**
 * @(#)CreatorOnKedacom.java 2011-11-7 Copyright 2011 it.kedacom.com, Inc. All
 *                           rights reserved.
 */

package com.kedacom.xmpp.demo.crossserver.pubsub;

import java.util.List;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

/**
 * (用一句话描述类的主要功能)
 * @author xiaojinghai
 * @date 2011-11-7
 */

public class ListenerOnQQ {

	private final String serverDomain = "qq.local";
	private final String pubsubServer = "pubsub.kedacom.local";
	private final String userName = "admin";
	private final String password = "itserver";
	private final String nodeName = "weiboNews";
	private final String resourceName = "java";
	private Connection connection;
	private PubSubManager pubSubManager;
	private LeafNode node;

	public String getJid() {
		return userName + "@" + serverDomain + "/" + resourceName;
	}

	public void connect() {
		connection = new XMPPConnection(serverDomain);
		try {
			connection.connect();
			connection.login(userName, password, resourceName);
			System.out.println("------------------" + getJid() + "已登录------------------");
		} catch (XMPPException e) {
			XMPPError error = e.getXMPPError();
			System.out.println(error.getCode());
			throw new RuntimeException("登录失败", e);
		} catch (Exception e) {
			throw new RuntimeException("登录失败", e);
		}

	}

	public void listenToNode() {
		pubSubManager = new PubSubManager(connection, pubsubServer);
		try {
			node = (LeafNode) pubSubManager.getNode(nodeName);
			// 接收信息
			node.addItemEventListener(new ItemEventListener<PayloadItem<SimplePayload>>() {

				public void handlePublishedItems(ItemPublishEvent<PayloadItem<SimplePayload>> event) {

					System.out.println(getJid() + "收到延迟消息:" + event.isDelayed());
					List<PayloadItem<SimplePayload>> items = event.getItems();
					for (PayloadItem<SimplePayload> item : items) {
						SimplePayload payload = item.getPayload();
						System.out.println("payload xml:" + payload.toXML());
					}
					System.out.println("------------------");
				}
			});
			node.subscribe(userName + "@" + serverDomain);
			System.out.println("正在侦听：" + pubsubServer + "/" + nodeName);
		} catch (XMPPException e) {
			// TODO 尚未处理异常
			e.printStackTrace();
		}

	}

	public void holdOn() {
		while (true) {// 驻留主线程
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO 尚未处理异常
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {

		ListenerOnQQ listener = new ListenerOnQQ();
		listener.connect();
		listener.listenToNode();

		while (true) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO 尚未处理异常
				e.printStackTrace();
			}
		}

		// Connection.DEBUG_ENABLED = true;

		// Thread t = new Thread(new Runnable() {
		//
		// public void run() {
		// String nodeName = "blogs";
		// Connection conn = connect("kedacom.local", "xiaojinghai", "888",
		// "java");
		// listenToNode(conn, "pubsub.kedacom.local", nodeName);
		//
		// }
		// }, "ListenerOnQQ");
		// t.setDaemon(true);
	}
}
